﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using ComponentAddRoomInfo.Forms;
using ComponentAddRoomInfo.RoomRoofModule;
using YArchitech.Controls;
using YArchitech.Revit;
using YJKView;

namespace ComponentAddRoomInfo.Command
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdAttachRoomName : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string messages, ElementSet elements)
		{
			this.doc = cmdData.Application.ActiveUIDocument.Document;
			int num = 0;
			int num2 = Convert.ToInt32(cmdData.Application.Application.VersionNumber);
			if (num2 < 2014)
			{
				messages = "此功能只在Revit2014及其以上版本可用。";
				return Autodesk.Revit.UI.Result.Failed;
			}
			CmdViewType types = (CmdViewType)11;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			bool flag = true;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
			IList<Element> list = filteredElementCollector.OfClass(typeof(SpatialElement)).ToElements();
			foreach (Element element in list)
			{
				if (element is Room && (element as Room).Location != null)
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				YJKMessageBox.Show("请先布置房间");
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			ComponentSelectForm componentSelectForm = new ComponentSelectForm();
			AddDataToEachType addDataToEachType = new AddDataToEachType(cmdData);
			if (DialogResult.OK == componentSelectForm.ShowDialog())
			{
				foreach (string text in componentSelectForm.m_lstComponentType)
				{
					string key;
					switch (key = text)
					{
					case "WallType":
						num += addDataToEachType.AddDataToWallType();
						break;
					case "FloorType":
						num += addDataToEachType.AddDataToFloorOrCeilingType("FloorType");
						break;
					case "ColumnType":
						num += addDataToEachType.AddDataToColumnType();
						break;
					case "CeilingType":
						num += addDataToEachType.AddDataToFloorOrCeilingType("CeilingType");
						break;
					case "DecorateType":
						num += addDataToEachType.AddDataToWallDecorateType();
						break;
					case "OtherType":
						num += addDataToEachType.AddDataToOtherType();
						break;
					}
				}
				List<Element> list2 = new List<Element>();
				Transaction transaction = new Transaction(this.doc, "CreateGroups");
				transaction.Start();
				foreach (KeyValuePair<Group, List<ElementId>> keyValuePair in addDataToEachType.dicEachGroupToEid)
				{
					foreach (ElementId elementId in keyValuePair.Value)
					{
						list2.Add(this.doc.GetElement(elementId));
					}
					this.CreateGroup(list2);
				}
				transaction.Commit();
				if (num != 0)
				{
					string message = "已为" + num.ToString() + "个构件添加房间属性。";
					YJKMessageBox.Show(message);
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public void CreateGroup(List<Element> elems)
		{
			ElementSet elementSet = new ElementSet();
			foreach (Element element in elems)
			{
				if (element != null)
				{
					elementSet.Insert(element);
				}
			}
			this.doc.CreatYJKGroup(elementSet);
		}

		private Document doc;
	}
}
